package com.example.demo;

import com.example.demo.aop.action.HelloController;
import com.example.demo.ioc.Student;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.annotation.MapperScans;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

import java.util.Arrays;

@SpringBootApplication
@EnableAspectJAutoProxy
@MapperScans(value = {@MapperScan("com.example.demo.mybatis.mapper"),
@MapperScan("com.example.demo.app2")
})
public class DemoApplication implements CommandLineRunner {

    public static void main(String[] args) {
    // 使用SpringApplication.run方法启动Spring Boot应用，DemoApplication.class是主应用类，args是传递的命令行参数
        SpringApplication.run(DemoApplication.class, args);

    // 以下代码被注释掉了，不会被执行
    // 创建一个Student对象
//        Student stu = new Student();
    // 设置Student对象的名字为"John"
//        stu.setName("John");
    // 设置Student对象的年龄为20
//        stu.setAge(20);


    }

    // spring容器对象 Autowrid根据类型自动注入进来
    @Autowired
    ApplicationContext applicationContext;

    @Override
    public void run(String... args) throws Exception {
        // 从容器中获取对象
        Student stu = applicationContext.getBean(Student.class);
        stu.setName("John");
        stu.setAge(20);

        System.out.println(stu);
        System.out.println("---------容器中所有bean-----start-----------");

        Arrays.asList(applicationContext.getBeanDefinitionNames()).forEach(item -> {
            System.out.println(item  + "@@@@" + applicationContext.getBean(item));
        });



        System.out.println("-----------容器中所有bean----end----------");

        HelloController helloController = applicationContext.getBean(HelloController.class);
        helloController.hello();
    }
}
